home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / tools / xglinfo / stringConversion.c.z / stringConversion.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  8.4 KB  |  296 lines

  1. /*
  2.  * 
  3.  *           Copyright (c) Digital Equipment Corporation, 1995
  4.  * 
  5.  *                          All Rights Reserved
  6.  * 
  7.  * Permission to use, copy, modify, and distribute  this software and its
  8.  * documentation for any  purpose   and without fee  is  hereby  granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both  that  copyright  notice  and  this  permission notice appear  in
  11.  * supporting documentation, and that the name of Digital  not be used in
  12.  * advertising or publicity  pertaining to distribution  of the  software
  13.  * without specific, written prior permission.
  14.  * 
  15.  * DIGITAL DISCLAIMS   ALL  WARRANTIES WITH   REGARD   TO  THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17.  * EVENT   SHALL  DIGITAL  BE   LIABLE  FOR  ANY SPECIAL,   INDIRECT   OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION  OF CONTRACT, NEGLIGENCE OR
  20.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21.  * PERFORMANCE OF THIS SOFTWARE.
  22.  * 
  23.  */
  24. /*
  25.  * HISTORY
  26.  */
  27. /*****************************************************************************/
  28. /******************************** Documentation ******************************/
  29. /*****************************************************************************/
  30.  
  31. /*****************************************************************************/
  32. /******************************* Include Files *******************************/
  33. /*****************************************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40. #include <GL/gl.h>
  41. #include <GL/glx.h>
  42. #include "global.h"
  43.  
  44. /*****************************************************************************/
  45. /****************************** Internal Defines *****************************/
  46. /*****************************************************************************/
  47.  
  48. /*****************************************************************************/
  49. /************************** Internal Type Definitions ************************/
  50. /*****************************************************************************/
  51.  
  52. typedef struct {
  53.   long Key;
  54.   char *String;
  55. } NumberedStrings;
  56.  
  57.  
  58. /*****************************************************************************/
  59. /**********************  External Function Declarations  *********************/
  60. /*****************************************************************************/
  61.  
  62. /*****************************************************************************/
  63. /**********************  Internal Function Declarations  *********************/
  64. /*****************************************************************************/
  65.  
  66. /*****************************************************************************/
  67. /*************************  External Global Variables  ***********************/
  68. /*****************************************************************************/
  69.  
  70. /*****************************************************************************/
  71. /*************************  Internal Global Variables  ***********************/
  72. /*****************************************************************************/
  73.  
  74. /*****************************************************************************/
  75. /***************************  Internal Functions  ****************************/
  76. /*****************************************************************************/
  77.  
  78. static void
  79. HeapSort(
  80. long     n,
  81. NumberedStrings x[]
  82. )
  83. {
  84.   long i;            /* array index */
  85.   long j;            /* array index */
  86.   long k;            /* array index */
  87.   NumberedStrings y;        /* temp element for swapping */
  88.  
  89.   /*
  90.    * heap sort only works if n is greater than or equal to 3, so if n < 3
  91.    * sort trivally
  92.    */
  93.   if (n == 1)
  94.     return;
  95.   if (n == 2) {
  96.     if (x[0].Key > x[1].Key) {
  97.       y = x[0];
  98.       x[0] = x[1];
  99.       x[1] = y;
  100.     }
  101.     return;
  102.   }
  103.   /* create initial heap */
  104.   for (k = 1; k < n; k++) {
  105.     /* insert x[k] into existing heap of size k-1 */
  106.     i = k;
  107.     y = x[k];        /* y is node to insert */
  108.     j = (i - 1) / 2;        /* j is the father of i */
  109.     while (i >= 1) {
  110.       if (y.Key <= x[j].Key)
  111.     break;
  112.       x[i] = x[j];
  113.       i = j;
  114.       j = (i - 1) / 2;
  115.     }
  116.     x[i] = y;
  117.   }
  118.  
  119.   /*
  120.    * We remove x[0] and place it in its proper position in the array. We then
  121.    * adjust the heap
  122.    */
  123.   for (k = n - 1; k > 0; k--) {
  124.     y = x[k];
  125.     x[k] = x[0];
  126.     /*
  127.      * Readjust the heap of order k-1. Move y down the heap for proper
  128.      * position
  129.      */
  130.     i = 0;
  131.     j = 1;
  132.     if ((k - 1 >= 2) && (x[2].Key > x[1].Key))
  133.       j = 2;
  134.     /* j is the larger son of i in the heap of size k-1 */
  135.     while (j <= k - 1) {
  136.       if (x[j].Key <= y.Key)
  137.     break;
  138.       x[i] = x[j];
  139.       i = j;
  140.       j = (2 * i) + 1;
  141.       if (j + 1 <= k - 1)
  142.     if (x[j + 1].Key > x[j].Key)
  143.       j++;
  144.     }
  145.     x[i] = y;
  146.   }
  147. }
  148.  
  149.  
  150. static char *BinarySearch (
  151.   long Key,
  152.   NumberedStrings *Table,
  153.   long TableSize
  154. )
  155. {
  156.   long Low = 0;
  157.   long High = TableSize - 1;
  158.   long Mid;
  159.   static char ErrString[80];
  160.  
  161.   while (Low <= High) {
  162.     Mid = (Low + High) / 2;
  163.     if (Key == Table[Mid].Key) return (Table[Mid].String);
  164.     if (Key < Table[Mid].Key)
  165.       High = Mid - 1;
  166.     else
  167.       Low = Mid + 1;
  168.   }
  169.   sprintf(ErrString, "Key Unknown %d", Key);
  170.   return (ErrString);
  171. }
  172.  
  173. /*****************************************************************************/
  174. /****************************  Exported Functions  ***************************/
  175. /*****************************************************************************/
  176.  
  177. char *VisualClassName(
  178.   long Key
  179. )
  180. {
  181.   static NumberedStrings Names[] = {
  182.     {StaticGray,    "StaticGray"},
  183.     {GrayScale,        "GrayScale"},
  184.     {StaticColor,    "StaticColor"},
  185.     {PseudoColor,    "PseudoColor"},
  186.     {TrueColor,        "TrueColor"},
  187.     {DirectColor,    "DirectColor"},
  188.   };
  189.   static int Sorted = 0;    /* not sorted yet */
  190.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  191.  
  192.   if (!Sorted) {
  193.     Sorted = 1;
  194.     HeapSort(N, Names);
  195.   }
  196.   return(BinarySearch(Key, Names, N));
  197. }
  198.  
  199. char *EventMaskName(
  200.   long Key
  201. )
  202. {
  203.   static NumberedStrings Names[] = {
  204.     {NoEventMask,        "NoEvent"},
  205.     {KeyPressMask,        "KeyPress"},
  206.     {KeyReleaseMask,        "KeyRelease"},
  207.     {ButtonPressMask,        "ButtonPress"},
  208.     {ButtonReleaseMask,        "ButtonRelease"},
  209.     {EnterWindowMask,        "EnterWindow"},
  210.     {LeaveWindowMask,        "LeaveWindow"},
  211.     {PointerMotionMask,        "PointerMotion"},
  212.     {PointerMotionHintMask,    "PointerMotionHint"},
  213.     {Button1MotionMask,        "Button1Motion"},
  214.     {Button2MotionMask,        "Button2Motion"},
  215.     {Button3MotionMask,        "Button3Motion"},
  216.     {Button4MotionMask,        "Button4Motion"},
  217.     {Button5MotionMask,        "Button5Motion"},
  218.     {ButtonMotionMask,        "ButtonMotion"},
  219.     {KeymapStateMask,        "KeymapState"},
  220.     {ExposureMask,        "Exposure"},
  221.     {VisibilityChangeMask,    "VisibilityChange"},
  222.     {StructureNotifyMask,    "StructureNotify"},
  223.     {ResizeRedirectMask,    "ResizeRedirect"},
  224.     {SubstructureNotifyMask,    "SubstructureNotify"},
  225.     {SubstructureRedirectMask,    "SubstructureRedirect"},
  226.     {FocusChangeMask,        "FocusChange"},
  227.     {PropertyChangeMask,    "PropertyChange"},
  228.     {ColormapChangeMask,    "ColormapChange"},
  229.     {OwnerGrabButtonMask,    "OwnerGrabButton"},
  230.   };
  231.   static int Sorted = 0;    /* not sorted yet */
  232.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  233.  
  234.   if (!Sorted) {
  235.     Sorted = 1;
  236.     HeapSort(N, Names);
  237.   }
  238.   return(BinarySearch(Key, Names, N));
  239. }
  240.  
  241. char *ByteOrderName(
  242.   long Key
  243. )
  244. {
  245.   static NumberedStrings Names[] = {
  246.     {LSBFirst,    "LSBFirst"},
  247.     {MSBFirst,    "MSBFirst"},
  248.   };
  249.   static int Sorted = 0;    /* not sorted yet */
  250.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  251.  
  252.   if (!Sorted) {
  253.     Sorted = 1;
  254.     HeapSort(N, Names);
  255.   }
  256.   return(BinarySearch(Key, Names, N));
  257. }
  258.  
  259. char *WindowFocusName(
  260.   long Key
  261. )
  262. {
  263.   static NumberedStrings Names[] = {
  264.     {None,        "None"},
  265.     {PointerRoot,    "PointerRoot"},
  266.   };
  267.   static int Sorted = 0;    /* not sorted yet */
  268.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  269.  
  270.   if (!Sorted) {
  271.     Sorted = 1;
  272.     HeapSort(N, Names);
  273.   }
  274.   return(BinarySearch(Key, Names, N));
  275. }
  276.  
  277. char *WindowFocusRevertName(
  278.   long Key
  279. )
  280. {
  281.   static NumberedStrings Names[] = {
  282.     {RevertToNone,        "RevertToNone"},
  283.     {RevertToPointerRoot,    "RevertToPointerRoot"},
  284.     {RevertToParent,        "RevertToParent"},
  285.   };
  286.   static int Sorted = 0;    /* not sorted yet */
  287.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  288.  
  289.   if (!Sorted) {
  290.     Sorted = 1;
  291.     HeapSort(N, Names);
  292.   }
  293.   return(BinarySearch(Key, Names, N));
  294. }
  295.  
  296.